package com.hz.sdka.sdka;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.ATNetworkConfirmInfo;
import com.anythink.core.api.AdError;
import com.anythink.splashad.api.ATSplashAd;
import com.anythink.splashad.api.ATSplashAdExtraInfo;
import com.anythink.splashad.api.ATSplashExListener;
import com.hz.sdka.commona.api.splash.HZSplash;
import com.hz.sdka.commona.api.splash.OnHZSplashListener;
import com.hz.sdka.commona.base.BaseAd;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.type.HZAdType;
import com.hz.sdka.commona.utils.LogUtils;

public class AdSplasha extends BaseAd implements HZSplash {

    private String TAG = "[splash] ===>";

    private ATSplashAd mATSplashAd;

    private OnHZSplashListener mOnHZSplashListener;


    public AdSplasha(Context activity, String placeId) {
        super(placeId, HZAdType.SPLASH.indexOf());
        mATSplashAd = new ATSplashAd(activity, placeId, new ATSplashExListener() {
            @Override
            public void onDeeplinkCallback(ATAdInfo atAdInfo, boolean b) {

            }

            @Override
            public void onDownloadConfirm(Context context, ATAdInfo atAdInfo, ATNetworkConfirmInfo atNetworkConfirmInfo) {

            }

            @Override
            public void onAdLoaded(boolean isTimeout) {
                LogUtils.d(TAG, "[onAdLoaded] isTimeout:" + isTimeout);
                if (!isTimeout && mOnHZSplashListener != null) {
                    mOnHZSplashListener.onSplashLoaded(isTimeout);
                }
            }

            @Override
            public void onAdLoadTimeout() {
                LogUtils.d(TAG, "[onAdLoadTimeout]");
                if (mOnHZSplashListener != null) {
                    mOnHZSplashListener.onSplashLoadFailed(HZAdError.AD_LOAD_TIMEOUT);
                }
            }

            @Override
            public void onNoAdError(AdError adError) {
                LogUtils.d(TAG, "[onNoAdError] code:" + adError.getCode() + "  msg:" + adError.getDesc());
                if (mOnHZSplashListener != null) {
                    mOnHZSplashListener.onSplashLoadFailed(new HZAdError(adError.getCode(),
                            adError.getDesc(),
                            adError.getPlatformCode(),
                            adError.getPlatformMSG()));
                }

                Bundle bundle = new Bundle();
                bundle.putString("code", adError.getCode() + "");
                bundle.putString("msg", adError.getDesc());
            }

            @Override
            public void onAdShow(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onAdShow]");
                if (mOnHZSplashListener != null) {
                    hzAdInfo.setEcpm(String.valueOf(atAdInfo.getEcpm()));
                    hzAdInfo.setNetworkPlacementId(atAdInfo.getNetworkPlacementId());
                    hzAdInfo.setNetworkType(atAdInfo.getAdNetworkType());
                    hzAdInfo.setShowId(atAdInfo.getShowId());
                    mOnHZSplashListener.onSplashShown(hzAdInfo);
                }
            }

            @Override
            public void onAdClick(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onAdClick]");
            }


            @Override
            public void onAdDismiss(ATAdInfo atAdInfo, ATSplashAdExtraInfo atSplashAdExtraInfo) {
                LogUtils.d(TAG, "[onAdDismiss]");
                if (mOnHZSplashListener != null) {
                    mOnHZSplashListener.onSplashClosed();
                }
            }
        });
    }


    @Override
    public void load() {
        mATSplashAd.loadAd();
    }

    @Override
    public void setListener(OnHZSplashListener listener) {
        this.mOnHZSplashListener = listener;
    }


    @Override
    public void show(Activity mActivity, ViewGroup viewGroup, String scenarioId) {
        if (mActivity == null || mActivity.isFinishing() || mActivity.isDestroyed()) {
            if (mOnHZSplashListener != null) {
                mOnHZSplashListener.onSplashShowFailed(HZAdError.ACTIVITY_IS_INVALID);
            }
            return;
        }

        if (!TextUtils.isEmpty(scenarioId)) {
            mATSplashAd.show(mActivity, viewGroup, scenarioId);
        } else {
            mATSplashAd.show(mActivity, viewGroup);
        }
    }


    /**
     * 获取广告价格
     */
    public double getEcpm() {
        if (mATSplashAd != null && mATSplashAd.isAdReady()) {
            return mATSplashAd.checkAdStatus().getATTopAdInfo().getEcpm();
        }
        return 0.00;
    }


    @Override
    public boolean isReady() {
        if (mATSplashAd != null)
            return mATSplashAd.isAdReady();
        else return false;
    }
}
